package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.itheima.dao.ESManagerMapper;

import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogInfo;
import com.itheima.service.BlogService;
import com.itheima.service.ESManagerService;
import com.itheima.service.getAllBlogService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ESManagerServiceImpl implements ESManagerService {
    @Autowired
    private ESManagerMapper esManagerMapper;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private getAllBlogService getAllBlogService;

    @Autowired
    private BlogService blogService;

    //创建索引库
    @Override
    public void initElasticSearchIndex() {
        elasticsearchTemplate.createIndex(BlogInfo.class);
        elasticsearchTemplate.putMapping(BlogInfo.class);
    }

    //导入全部数据
    @Override
    public void importAll() {
        List<Blog> blogList = getAllBlogService.getAll();
        if(blogList==null||blogList.size()<=0){
            throw new RuntimeException("当前没有数据，无法导入索引库");
        }
        //把list<blog>转换成list<bloginfo>
        String blogJson = JSON.toJSONString(blogList);
        List<BlogInfo> blogInfoList = JSON.parseArray(blogJson, BlogInfo.class);
        esManagerMapper.saveAll(blogInfoList);
    }

   // 根据uid导入数据
    @Override
    public void importByUid(String uid) {
        Blog blog=blogService.getBlogByUid(uid);
        if(blog==null){
            throw new RuntimeException("当前没有数据，无法导入索引库");
        }
        String blogStr = JSON.toJSONString(blog);
        BlogInfo blogInfo = JSON.parseObject(blogStr,BlogInfo.class);
        esManagerMapper.save(blogInfo);
    }

    //根据uid删除数据
    @Override
    public void deleteByUid(String uid) {
        esManagerMapper.deleteById(uid);
    }

    //根据uids删除数据
    @Override
    public void deleteByUids(String uids) {
       String[] Uids=uids.split(",");
        for (String uid : Uids) {
            esManagerMapper.deleteById(uid);
        }
    }

    //通过es搜索博客
    @Override
    public List<BlogInfo> getByKeywords(String keywords, Integer currentPage, Integer pageSize) {
        //构建搜索条件
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        //根据文本内容匹配搜索
        boolQueryBuilder.should(QueryBuilders.queryStringQuery(keywords).defaultField("content"));

        //分页
        Pageable pageable= PageRequest.of(currentPage-1,pageSize);

        //构建查询
        SearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(pageable)
                .build();
        Page<BlogInfo> blogInfoPage = esManagerMapper.search(query);

        return blogInfoPage.getContent();
    }

    //搜索博客
    @Override
    public Page<BlogInfo> sqlSearchBlog(String keyWords, Integer currentPage, Integer pageSize) {
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
        //根据标题分类
        boolQueryBuilder.should(QueryBuilders.termQuery("titles",keyWords));
        //根据介绍分类
        boolQueryBuilder.should(QueryBuilders.termQuery("summary",keyWords));
        //根据文本内容匹配
        boolQueryBuilder.should(QueryBuilders.queryStringQuery(keyWords).defaultField("content"));
        //分页
        Pageable pageable= PageRequest.of(currentPage-1,pageSize);
        SearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(pageable)
                .build();
        Page<BlogInfo> blogInfoPage = esManagerMapper.search(query);

        return blogInfoPage;
    }

    //根据作者获取相关的博客
    @Override
    public List<BlogInfo> searchBlogByAuthor(String author, Integer currentPage, Integer pageSize) {
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
        boolQueryBuilder.should(QueryBuilders.queryStringQuery(author).defaultField("author"));
        Pageable pageable= PageRequest.of(currentPage-1,pageSize);
        SearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(pageable)
                .build();
        Page<BlogInfo> blogInfoPage = esManagerMapper.search(query);

        return blogInfoPage.getContent();

    }

    //根据分类获取相关的博客
    @Override
    public List<BlogInfo> searchBlogBySort(String blogSortUid, Integer currentPage, Integer pageSize) {
        //构建搜索条件
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        //根据分类匹配搜索
        boolQueryBuilder.should(QueryBuilders.termQuery("blogSortUid",blogSortUid));

        //分页
        Pageable pageable= PageRequest.of(currentPage-1,pageSize);

        //构建查询
        SearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(pageable)
                .build();
        Page<BlogInfo> blogInfoPage = esManagerMapper.search(query);

        return blogInfoPage.getContent();
    }

    //根据标签获取相关的博客
    @Override
    public List<BlogInfo> searchBlogByTag(String tagUid, Integer currentPage, Integer pageSize) {
        //构建搜索条件
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        //根据标签匹配搜索
        boolQueryBuilder.should(QueryBuilders.termQuery("tagUid",tagUid));

        //分页
        Pageable pageable= PageRequest.of(currentPage-1,pageSize);

        //构建查询
        SearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(pageable)
                .build();
        Page<BlogInfo> blogInfoPage = esManagerMapper.search(query);

        return blogInfoPage.getContent();
    }
}
